import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * used to load/save from the database
 * @author paul peasegood
 *
 */
public class Database 
{
	
	/**
	 * loads a player from the database
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public static Player loadPlayer(String name) throws SQLException
	{
		Connection con = null;
	    Statement stmt = null;
	    ResultSet rs = null;
	    String sql;
	    int HPmax;
	    double HPgen;
	    int HPfatigue;
	    int APmax;
	    int APgen;
	    int dodge;
	    int energyMax;
	    double energyGen;

	    try 
	    {
	      Class.forName("org.sqlite.JDBC");
	      con = DriverManager.getConnection("jdbc:sqlite:/home/paul/programing/final-year/database/Player.sqlite");
	    } 
	    catch ( Exception e )
	    {
	      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
	      System.exit(0);
	    }
	    sql = "SELECT * FROM player WHERE name = '" + name + "'";
	    stmt = con.createStatement();
	    rs = stmt.executeQuery(sql);
	    System.out.println(rs.getString("name") + " HP:" + rs.getInt("HP"));
	    HPmax = rs.getInt("HP");
	    HPgen = rs.getDouble("HPgen");
	    HPfatigue = rs.getInt("HPfatigue");
	    APmax = rs.getInt("APmax");
	    APgen = rs.getInt("APgen");
	    dodge = rs.getInt("dodge");
	    energyMax = rs.getInt("energyMax");
	    energyGen = rs.getDouble("energyGen");
	    con.close();
	    return new Player(name, HPmax, HPgen,HPfatigue, APmax, APgen, energyMax, energyGen, dodge);
	}
	
	/**
	 * loads a weapon from the database
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public static Weapon loadWeapon(String key) throws SQLException
	{
		Connection con = null;
	    Statement stmt = null;
	    ResultSet rs = null;
	    String sql;


	    try 
	    {
	      Class.forName("org.sqlite.JDBC");
	      con = DriverManager.getConnection("jdbc:sqlite:/home/paul/programing/final-year/database/Player.sqlite");
	    } 
	    catch ( Exception e )
	    {
	      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
	      System.exit(0);
	    }
	    sql = "SELECT * FROM weapon WHERE key = '" + key + "'";
	    stmt = con.createStatement();
	    rs = stmt.executeQuery(sql);
	    
	    int toHit = rs.getInt("toHit");
	    int diceNum = rs.getInt("diceNum");
	    int diceSides = rs.getInt("diceSides");
	    int damBonus = rs.getInt("damBonus");
	    int apCost = rs.getInt("useCost");
	    int energyCost = rs.getInt("energyCost");
	    int weight = rs.getInt("weight");
	    con.close();
	    return new Weapon(toHit, diceNum, diceSides, damBonus, apCost,energyCost, weight);
	}
	
	public static PosionWeapon loadPosionWeapon(String key) throws SQLException
	{
		Connection con = null;
	    Statement stmt = null;
	    ResultSet rs = null;
	    String sql;


	    try 
	    {
	      Class.forName("org.sqlite.JDBC");
	      con = DriverManager.getConnection("jdbc:sqlite:/home/paul/programing/final-year/database/Player.sqlite");
	    } 
	    catch ( Exception e )
	    {
	      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
	      System.exit(0);
	    }
	    sql = "SELECT * FROM posionWeapon WHERE key = '" + key + "'";
	    stmt = con.createStatement();
	    rs = stmt.executeQuery(sql);
	    
	    int toHit = rs.getInt("toHit");
	    int diceNum = rs.getInt("diceNum");
	    int diceSides = rs.getInt("diceSides");
	    int damBonus = rs.getInt("damBonus");
	    int useCost = rs.getInt("useCost");
	    int energyCost = rs.getInt("energyCost");
	    int weight = rs.getInt("weight");
	    int posNum = rs.getInt("posNum");
	    int posSides = rs.getInt("posSides");
	    int posBonus = rs.getInt("posBonus");
	    int turns = rs.getInt("turns");
	    con.close();
	    return new PosionWeapon(toHit, diceNum, diceSides, damBonus, useCost,energyCost, weight,posNum,posSides,posBonus,turns);
	}
	/**
	 * loads a armor from the database
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public static Armor loadArmor(String key) throws SQLException
	{
		Connection con = null;
	    Statement stmt = null;
	    ResultSet rs = null;
	    String sql;
	    int damReduce;
	    int dodgePen;
	    int HPmax;

	    try 
	    {
	      Class.forName("org.sqlite.JDBC");
	      con = DriverManager.getConnection("jdbc:sqlite:/home/paul/programing/final-year/database/Player.sqlite");
	    } 
	    catch ( Exception e )
	    {
	      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
	      System.exit(0);
	    }
	    sql = "SELECT * FROM armor WHERE key = '" + key + "'";
	    stmt = con.createStatement();
	    rs = stmt.executeQuery(sql);
	    
	    damReduce = rs.getInt("damReduce");
	    dodgePen = rs.getInt("dodgePen");
	    HPmax = rs.getInt("HPmax");

	    con.close();
	    return new Armor(damReduce, dodgePen, HPmax);
	}

}
